/*****************************************************************************************
 * Aware : an Asynchonous Aware-Oriented Software Architecture
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.aware;

import java.util.Hashtable;
import java.util.Iterator;

import dexter.aware.policy.SelectorPolicy;

/**
 * Selector static util class
 * 
 * @author Ruben Mondejar <rmondejara@uoc.edu>
 */
public class Selector {

	public static Hashtable<String,int[]> selectHosts(int num, SelectorPolicy<int[]> policy) {
		
		Hashtable<String,int[]> hosts = Monitor.getPool();
		Hashtable<String,int[]> filtered = policy.filter(hosts);
		Hashtable<String,int[]> choosen = new Hashtable<String,int[]>();
		Iterator<String> it = filtered.keySet().iterator();
		while(choosen.size()<num && it.hasNext()) {
			String hostname = it.next();
			int[] state = filtered.get(hostname);
			choosen.put(hostname, state);
		}
		return choosen;
	}

	public static Hashtable<String,int[]> selectHostsByTime(int num, int hours, SelectorPolicy<int[]> policy) {
		
		Hashtable<String,int[]> hosts = Monitor.getPool(hours);
		Hashtable<String,int[]> filtered = policy.filter(hosts);
		Hashtable<String,int[]> choosen = new Hashtable<String,int[]>();
		Iterator<String> it = filtered.keySet().iterator();
		while(choosen.size()<num && it.hasNext()) {
			String hostname = it.next();
			int[] state = filtered.get(hostname);
			choosen.put(hostname, state);
		}
		return choosen;
	}
	
//	//TEST
//	public static void main(String[] args) {
//		final int PORT = Configuration.getFrontPort();
//		
//		//START NEW MONITOR
//		new Thread() {
//		  public void run() {	
//		    new Monitor(PORT);
//		  }  
//		}.start();  
//		
//		try {
//		  Thread.sleep(5000);
//		} catch (Exception e) {}
//		  
//		int NUM = 10;
//		StatePolicy policy = new StatePolicy(new int[]{25,25,25});
//		
//		Hashtable<String,int[]>  hosts = Selector.selectHosts(NUM, policy);
//		System.out.println("Test "+policy+": ");
//		for (String hostname : hosts.keySet()) {
//			int[] state = hosts.get(hostname); 
//			System.out.println(hostname+" cpu% : "+state[0]+", mem% : "+state[1]+", disk% : "+state[2]+"");
//		}
//		
//		Hashtable<String,int[]>  hosts2 = Selector.selectHostsByTime(NUM, 2, policy);
//		System.out.println("Test2 "+policy+": ");
//		for (String hostname : hosts2.keySet()) {
//			int[] state = hosts2.get(hostname); 
//			System.out.println(hostname+" cpu% : "+state[0]+", mem% : "+state[1]+", disk% : "+state[2]+"");
//		}	
//		
//	}
}


